<template>
  <div id="listQuestion">
    <a-card style="margin-bottom: 20px" hoverable>
      <a-form :model="queryParams" layout="inline">
        <a-form-item field="title" label="题目">
          <a-input
            v-model="queryParams.title"
            placeholder="请输入题目"
            :style="{ width: '270px', marginRight: '20px' }"
          />
        </a-form-item>
        <a-form-item field="content" label="内容">
          <a-input
            v-model="queryParams.content"
            placeholder="请输入内容"
            :style="{ width: '270px', marginRight: '20px' }"
          />
        </a-form-item>
        <a-form-item field="tags" label="标签">
          <a-input-tag
            v-model="queryParams.tags"
            placeholder="请输入标签"
            :style="{ width: '270px' }"
          />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="handleSearch">搜索</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-table
      :columns="columns"
      :data="data"
      :pagination="{
        showTotal: true,
        pageSize: queryParams.pageSize,
        current: queryParams.current,
        total: Number(total),
      }"
      @pageChange="handlePageChange"
    >
      <template #passRate="{ record }">
        {{
          record.submitNum === 0
            ? 0
            : `${record.acceptedNum / record.submitNum} %`
        }}
      </template>
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag
            v-for="(tag, index) of record.tags"
            :key="index"
            color="green"
            disabled
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD HH:mm:ss") }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="toDetail(record.id)">作答</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { ref, watchEffect } from "vue";
import { QuestionControllerService, QuestionQueryRequest } from "@/api";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const data = ref([]);
const total = ref(0);
const router = useRouter();
const queryParams = ref<QuestionQueryRequest>({
  current: 0,
  pageSize: 10,
});

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost({
    ...queryParams.value,
    sortField: "create_time",
    sortOrder: "descend",
  });
  if (res.code === 0) {
    data.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("系统错误" + res.message);
  }
};

//监听表格的变化
watchEffect(() => {
  loadData();
});

const columns = [
  {
    title: "ID",
    dataIndex: "id",
  },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "内容",
    dataIndex: "content",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过率",
    slotName: "passRate",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];

const toDetail = (id: number) => {
  router.push({
    path: "/detail/question/" + id,
  });
};

const handlePageChange = (page: number) => {
  queryParams.value = {
    ...queryParams.value,
    current: page,
  };
};
const handleSearch = () => {
  loadData();
};
</script>

<style scoped>
#listQuestion {
  max-width: 1200px;
  margin: 0 auto;
}
</style>
